import {
	initData,
	initData2
} from '@/common/api/index.js'
export const ListMixin = {
	data() {
		return {
			queryParam: {}, //传入参数
			/* 分页参数 */
			ipagination: {
				page: 1, //当前页
				size: 10, //每页多少条
				total: 0, //总条数
			},
			/* 分页参数 */
			dataSource: [], //数据源
			loadStatus: 'loadmore', //加载更多状态
			field: 'list',
		}
	},
	methods: {
		//上拉加载更多(待验证)
		reachBottom() {
			this.loadStatus = 'loading'
			// 当数据长度小于total总数时加载更多
			if (this.dataSource.length < this.ipagination.total) {
				++this.ipagination.page
				this.loadData()

			} else {
				this.loadStatus = "nomore"
			}
		},
		//加载数据
		async loadData() {
			let that = this
			let params = Object.assign(that.queryParam, {
				current: that.ipagination.page,
				page: 1,
				limit: that.ipagination.size
			})

			const res = await initData(that.urlapi.list, params, 'post')

			const data = res
			if (res) {
				that.ipagination.total = data.total
				that.dataSource = data[that.field] ? that.dataSource.concat(data[that.field]) : []
				if (that.dataSource.length > data.total || that.dataSource.length === data.total) {
					that.loadStatus = "nomore"
				} else {
					that.loadStatus = "loadmore"
				}
			}

		},
		goDetail(url, item) {
			// console.log("查看详情",params)
			// const params = JSON.stringify(item);
			// console.log("查看详情",params)
			this.$uniRouter.navigateTo({
				url: `${url}?id=${item.id}`
			})
		}
	}
}

export const submitMinxins = {
	data() {
		return {

		}
	},
	methods: {
		//加载数据
		async formSubmit() {
			let that = this
			console.log("保存", that.formData)
			var checkRes = that.$validateCheck.check(that.formData, that.rule)
			if (checkRes) {
				// isAdd 是否是增加
				if (this.isAdd) {
					this.postData()
				} else {
					this.putData()
				}
			} else {
				uni.showToast({
					title: that.$validateCheck.error,
					icon: "none"
				});
			}
		},
		async postData() {
			let that = this
			const res = await initData(that.urlapi.add, that.formData, 'post')
			if (res) {
				// goUtl 提交成功后跳转页面，没有默认返回上一页
				console.log("添加治疗")
				if (that.urlapi.goUtl) {
					that.$uniRouter.redirectTo({
						url: that.urlapi.goUtl
					})
				} else {
					uni.showToast({
						title: "提交成功",
						icon: "none",
					});
					setTimeout(() => {
						uni.$u.route({
							type: 'navigateBack'
						})
					}, 1000)

				}
			}
		},

		async putData() {
			let that = this
			const res = await initData2(that.urlapi.edit, that.formData, 'put')
			if (res.code === '0') {
				// goUtl 提交成功后跳转页面，没有默认返回上一页
				if (that.urlapi.goUtl) {
					that.$uniRouter.redirectTo({
						url: that.urlapi.goUtl
					})
				} else {
					uni.showToast({
						title: "编辑成功",
						icon: "none",
					});
					setTimeout(() => {
						uni.$u.route({
							type: 'navigateBack'
						})
					}, 1000)

				}
			}

		},

	}
}
export const detailMinxins = {
	data() {
		return {
			
		}
	},
	methods: {
		async getDetailData(id) {
			return new Promise((resolve,reject)=>{
				let that = this
				let requesUrl = that.urlapi.detail
				if (requesUrl) {
					initData(`${requesUrl}/${id}`, {}, 'get').then(res=>{
						resolve(res)	
					})
					
				}
			})
			
		},
		 deleteSubmit(id) {
			let that = this
			uni.showModal({
				title: '删除',
				content: '确定要删除吗？',
				success: async function(res) {
					if (res.confirm) {
						let requesUrl = that.urlapi.del
						if (requesUrl) {
							const res = await initData2(`${requesUrl}/${id}`, {}, 'delete')
							console.log("删除", res)
							if (res.code === '0') {
								uni.showToast({
									title: "删除成功",
									icon: "none",
								});
								setTimeout(() => {
									uni.$u.route({
										type: 'navigateBack'
									})
								}, 1000)
							}
						
						}
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			})
			
		}

	}
}
import {
	JsonParse,
	isAssetTypeAnImage
} from '@/common/utils/validate.js'
export const downMinxins = {
	methods: {
		downloadFiles(url) {
			// console.log("downloadFiles",url,isAssetTypeAnImage(url))
			//判断是否是图片文件，是则保存到相册
			if (url) {
				if (isAssetTypeAnImage(url)) {
					this.imgDownload(url)
				} else {
					this.fileDownload(url)
				}
			} else {
				uni.showToast({
					icon: 'none',
					mask: true,
					title: '暂无文件',
				});
			}
		},
		fileDownload(attachLink) {
			// console.log("文件下载", attachLink)
			//通用下载文件方法
			let that = this
			uni.downloadFile({
				url: attachLink, //下载地址，后端接口获取的链接 示例：http:192.168.0.2:8080/files/123.doc
				success: (data) => {
					console.log(data.tempFilePath)
					// console.log(JSON.stringify(data))
					if (data.statusCode === 200) {
						uni.saveFile({ //文件保存到本地
							tempFilePath: data.tempFilePath, //临时路径
							success: function(res) {
								console.log("下载成功" + res.savedFilePath)
								// console.log(JSON.stringify(res))
								uni.showToast({
									icon: 'none',
									mask: true,
									title: '文件已保存!',
									duration: 3000,
								});
								uni.openDocument({
									//fileType: 'docx',
									showMenu: true, //关键点,可以转发到微信
									filePath: res.savedFilePath,
									success: function(res) {
										console.log('打开文档成功');
									}
								});
							}
						});
					}
				},
				fail: (err) => {
					// console.log(err);
					uni.showToast({
						icon: 'none',
						mask: true,
						title: '失败请重新下载',
					});
				},
			});


		},
		//下载图片
		imgDownload(imgUtl) {
			var that = this
			//获取相册授权
			console.log("相册", imgUtl)
			uni.getSetting({
				success(res) {
					// console.log("getSetting",res)
					if (!res.authSetting['scope.writePhotosAlbum']) {
						uni.authorize({
							scope: 'scope.writePhotosAlbum',
							success() {
								//这里是用户同意授权后的回调
								that.saveImgToLocal(imgUtl);
							},
							fail() { //这里是用户拒绝授权后的回调

							}
						})
					} else { //用户已经授权过了
						that.saveImgToLocal(imgUtl);
					}
				}
			})
		},
		saveImgToLocal(imgUtl) {
			var that = this
			uni.showModal({
				title: '提示',
				content: '确定保存到相册吗',
				success: function(res) {
					if (res.confirm) {
						uni.downloadFile({
							url: imgUtl, //图片地址
							success: (res) => {
								if (res.statusCode === 200) {
									uni.saveImageToPhotosAlbum({
										filePath: res.tempFilePath,
										success: function() {
											uni.showToast({
												title: "保存成功",
												icon: "none"
											});
										},
										fail: function() {
											uni.showToast({
												title: "保存失败",
												icon: "none"
											});
										}
									});
								}
							}
						})
					} else if (res.cancel) {

					}
				}
			});
		},
		//下载图片 end
	}
}